<!DOCTYPE html>
<meta charset="utf-8">
<title>Navigation Preload Resource Timing after terminating SW</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>
<script>
function check_timing_entry(entry, allow_negative_value) {
  var name = entry.name;
  if (!allow_negative_value) {
      assert_greater_than(entry.startTime, 0, 'startTime of ' + name);
      assert_greater_than(entry.fetchStart, 0, 'fetchStart of ' + name);
      assert_greater_than(entry.domainLookupStart, 0,
                          'domainLookupStart of ' + name);
      assert_greater_than(entry.domainLookupEnd, 0,
                          'domainLookupEnd of ' + name);
      assert_greater_than(entry.connectStart, 0, 'connectStart of ' + name);
      assert_greater_than(entry.connectEnd, 0, 'connectEnd of ' + name);
      assert_greater_than(entry.requestStart, 0, 'requestStart of ' + name);
      assert_greater_than(entry.responseStart, 0, 'responseStart of ' + name);
      assert_greater_than(entry.responseEnd, 0, 'responseEnd of ' + name);
  }
  assert_greater_than_equal(entry.fetchStart, entry.startTime,
                            'fetchStart >= startTime of ' + name);
  assert_greater_than_equal(entry.domainLookupStart, entry.fetchStart,
                            'domainLookupStart >= fetchStart of ' + name);
  assert_greater_than_equal(entry.domainLookupEnd, entry.domainLookupStart,
                            'domainLookupEnd >= domainLookupStart of ' + name);
  assert_greater_than_equal(entry.connectStart, entry.domainLookupEnd,
                            'connectStart >= domainLookupEnd of ' + name);
  assert_greater_than_equal(entry.connectEnd, entry.connectStart,
                            'connectEnd >= connectStart of ' + name);
  assert_greater_than_equal(entry.requestStart, entry.connectEnd,
                            'requestStart >= connectEnd of ' + name);
  assert_greater_than_equal(entry.responseStart, entry.requestStart,
                            'domainLookupStart >= requestStart of ' + name);
  assert_greater_than_equal(entry.responseEnd, entry.responseStart,
                            'responseEnd >= responseStart of ' + name);
  assert_greater_than(entry.duration, 0, 'duration of ' + name);
}

promise_test(t => {
    var script = 'resources/navigation-preload-resource-timing-worker.js';
    var scope = 'resources/navigation-preload-resource-timing-scope.html';
    var registration;
    var frames = [];

    return service_worker_unregister_and_register(t, script, scope)
      .then(reg => {
          registration = reg;
          add_completion_callback(_ => registration.unregister());
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(_ => with_iframe(scope + '?1'))
      .then(frame => {
          frames.push(frame);
          return with_iframe(scope + '?2');
        })
      .then(frame => {
          frames.push(frame);
          return internals.terminateServiceWorker(registration.active);
        })
      .then(_ => with_iframe(scope + '?3'))
      .then(frame => {
          frames.push(frame);
          var results = frames.map(frame => {
            var result = JSON.parse(frame.contentDocument.body.textContent);
            return result.timingEntries.filter(entry => {
              return entry.name.indexOf(scope) != -1;
            })
          });
          assert_equals(results.length, 3);
          assert_equals(results[0].length, 1,
                        'The first result must contain only one entry.');
          assert_equals(results[0][0].name, frames[0].src,
                        'The entry of the first result must be the entry for ' +
                        'the first iframe.');
          assert_equals(results[1].length, 1,
                        'The second result must contain only one entry.');
          assert_equals(results[1][0].name, frames[1].src,
                        'The entry of the second result must be the entry ' +
                        'for the second iframe.');
          assert_equals(results[2].length, 1,
                        'The third result must contain only one entry.');
          assert_equals(results[2][0].name, frames[2].src,
                        'The entry of the third result must be the entry for ' +
                        'the third iframe.');
          check_timing_entry(results[0][0], false /* allow_negative_value */);
          check_timing_entry(results[1][0], false /* allow_negative_value */);
          check_timing_entry(results[2][0], true /* allow_negative_value */);
          frames.forEach(frame => frame.remove());
          return registration.unregister();
        });
  }, 'Navigation Preload Resource Timing after terminating SW.');

</script>
